home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7681 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  72 lines

  1. Newsgroups: comp.lang.c++
  2. Path: news.eds.co.nz!usenet
  3. From: Ross Smith <ross.smith@nz.eds.com>
  4. Subject: Re: STL
  5. Content-Type: text/plain; charset=us-ascii
  6. Sender: usenet@nz.eds.com (Usenet News Admin)
  7. Content-Transfer-Encoding: 7bit
  8. Organization: EDS (New Zealand) Ltd
  9. Message-ID: <312D41A0.19B7@nz.eds.com>
  10. References: <4gdgt0$494@netaxs.com> <312B2DEF.65C7@airmail.net>
  11. X-Mailer: Mozilla 2.0 (WinNT; I)
  12. Mime-Version: 1.0
  13. X-Nntp-Posting-Host: crazy-harry.lab.nz.eds.com
  14. Date: Fri, 23 Feb 1996 04:25:04 GMT
  15.  
  16. Mark Nelson wrote:
  17. > Bob Pesavento wrote:
  18. > >
  19. > > I'm a newbie with STL and not an expert at C++ so bear with me.  Assume a
  20. > > simplified class:
  21. > >
  22. > > class myclass
  23. > > {
  24. > >   int myint;
  25. > >   float myfloat;
  26. > >
  27. > >   public:...
  28. > >
  29. > > I want use "list" and stuff a bunch of these in using push_back.  No
  30. > > problem so far.  But I want the user to be able to select which item to
  31. > > sort on... myint or myfloat.
  32. > The sorting is done using operator<(), so you don't have much choice about that.
  33. > [ bogus solution snipped ]
  34.  
  35. Not true; sort() can accept a comparison function instead of op<. You need
  36. to declare the appropriate comparison functions (which must be global
  37. functions, not members), something like this:
  38.  
  39.     class myclass {
  40.       private:
  41.         int myint;
  42.         float myfloat;
  43.       public:
  44.         // the usual crop of constructors etc
  45.       friend bool CompareByInt(const myclass& x, const myclass& y) {
  46.         return x.myint < y.myint;
  47.       }
  48.       friend bool CompareByFloat(const myclass& x, const myclass& y) {
  49.         return x.myfloat < y.myfloat;
  50.       }
  51.     };
  52.  
  53. (NB. Whether the friend functions are defined inside or outside the class
  54. body is unimportant; it's just a style choice.)
  55.  
  56. Then you can sort a sequence with something like:
  57.  
  58.     sort(sequence.begin(), sequence.end(), CompareByInt);
  59.     sort(sequence.begin(), sequence.end(), CompareByFloat);
  60.  
  61. However, you can't sort a list using sort(); it has to be a vector or
  62. deque (because sort() needs random-access iterators, while lists only
  63. have bidirectional iterators).
  64.  
  65. -- 
  66. Ross Smith ........................................ Wellington, New Zealand
  67. Work: <mailto:ross.smith@nz.eds.com> ... Home: <mailto:alien@netlink.co.nz>
  68. "Remember when we told you there was no future? Well, this is it."
  69.                                                                -- Blank Reg
  70.